// index.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import { setTabBarIndex } from '../../utils/hooks';
import subPub from '../../utils/subPub';
const $api = require('../../request/api').API;

const app = getApp();
Page({
  data: {
    menuBtnTop: app.globalData.menuBtnTop,
    // beeperId:"",//录音选中的beeperId
    localeId: '',
    localeName: '',
    localeMainFlag: 0,
    // localeState为1表示该场所可用,为0表示在审核
    localeState: 1,
    classActive: 1,
    callingList: [],
    // 切换用户弹框
    taggerUserShow: false,
    localeList: [],
    localeScrollTop: 0
    // 页面栈
    // currentPages:[]
  },
  onLoad(options) {
    this.sleepSyncDetectionPushId = subPub.on('sleepSyncDetectionPush', ({ deviceId }) => {
      const { callingList } = this.data;
      callingList.forEach(item => {
        if (item.type == 11 && item.equipMac == deviceId) item.state = 2;
      });
      this.setData({
        callingList
      });
    });
    // 监听设备离线
    this.deviceOfflineId = subPub.on('deviceOffline', deviceId => {
      const { callingList } = this.data;
      callingList.forEach(item => {
        if (item.type == 11 && item.equipMac == deviceId) item.state = 0;
      });
      this.setData({
        callingList
      });
    });
    // 监听设备在线
    this.deviceOnlineId = subPub.on('deviceOnline', deviceId => {
      const { callingList } = this.data;
      callingList.forEach(item => {
        if (item.type == 11 && item.equipMac == deviceId) item.state = 0;
      });
      this.setData({
        callingList
      });
    });
  },
  onShow() {
    setTabBarIndex(1);
    if (app.globalData.localeId || !this.alreadyCome) {
      // 如果app上面存在localeId说明是绑定设备页面来的或者分享页面来的需要，需要选中场所,或者是第一次进来
      this.localeList();
    } else if (app.globalData.refreshBeeperList) {
      this.localeList(1);
      delete app.globalData.refreshBeeperList;
    }
    this.alreadyCome = true;
  },
  // 去详情
  gotoDetails(e) {
    const { id, type, powerState } = e.currentTarget.dataset;
    const { localeId, localeMainFlag } = this.data;
    if (powerState == 0) return;
    if (localeMainFlag == 0 && type == 1) {
      Toast.fail('公共呼叫器下非主用户不可进入详情');
      return;
    }
    switch (type) {
      case 1:
        wx.navigateTo({
          url: '/subcontract/equipment/pages/beeperHome/index?beeperId=' + id
        });
        break;
      case 3:
        console.log('烟雾');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsY/index?beeperId=' + id
        });
        break;
      case 4:
        console.log('燃气');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsR/index?beeperId=' + id
        });
        break;
      case 5:
        console.log('门磁');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsM/index?beeperId=' + id
        });
        break;
      case 6:
        console.log('水浸');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsW/index?beeperId=' + id
        });
        break;
      case 7:
        console.log('摄像头');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsC/index?beeperId=' + id
        });
        break;
      case 8:
        console.log('SOS');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsS/index?beeperId=' + id
        });
        break;
      case 9:
        console.log('跌倒');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsD/index?beeperId=' + id
        });
        break;
      case 10:
        console.log('红外');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsH/index?beeperId=' + id
        });
        break;
      case 11:
        console.log('睡眠监测仪');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsL/index?beeperId=' + id
        });
        break;
      case 12:
        console.log('血糖仪');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsG/index?beeperId=' + id + '&localeId=' + localeId
        });
        break;
      case 13:
        console.log('血压仪');
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsP/index?beeperId=' + id + '&localeId=' + localeId
        });
        break;
      case 14:
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsDP/index?beeperId=' + id + '&localeId=' + localeId
        });
        break;
      case 15:
        wx.navigateTo({
          url: '/subcontract/equipment/pages/crutchHome/index?beeperId=' + id + '&localeId=' + localeId
        });
        break;
      case 16:
        wx.navigateTo({
          url: '/subcontract/equipment/pages/detailsNB/index?beeperId=' + id + '&localeId=' + localeId
        });
        break;
      case 17:
        wx.navigateTo({
          url: '/subcontract/equipment/pages/watchHome/index?beeperId=' + id + '&localeId=' + localeId
        });
				break;
				case 20:
					wx.navigateTo({
						url: '/subcontract/equipment/pages/locatingShoeHome/index?beeperId=' + id + '&localeId=' + localeId
					});
					break;
      default:
        break;
    }
  },
  // 去充值页
  toPay(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subcontract/equipment/pages/topUp/index?id=' + id
    });
  },
  // 获取呼叫器和其它设备列表数据
  async getList() {
    const res = await $api.listBeeperByCustomer({ localeId: this.data.localeId });
    this.setData({
      callingList: res.data.data
    });
  },
  async refreshLocale() {
    try {
      Toast.loading('加载中');
      await this.localeList();
    } catch (err) {
      Toast.clear();
    }
  },
  // 刷新呼叫器
  async replayData() {
    wx.showLoading({
      title: '刷新中...'
    });
    // 用户
    await this.getList();
    wx.hideLoading();
  },

  // 点击用户名
  clickTaggerUser() {
    this.setData({
      taggerUserShow: true
    });
  },
  // 关闭用户列表
  closeTaggerUser() {
    this.setData({
      taggerUserShow: false
    });
  },
  // 点击场所
  clickUser(e) {
    const { name, id, state, mainflag } = e.currentTarget.dataset;
    this.setData({
      localeName: name,
      localeId: id,
      localeState: state,
      localeMainFlag: Number(mainflag),
      taggerUserShow: false
    });
    this.getList();
  },
  // 去场所/用户列表
  toPlaceList(e) {
    const { type } = e.currentTarget.dataset;
    this.setData({
      taggerUserShow: false
    });
    if (type == 1) {
      if (this.data.localeState == 0) {
        Toast.fail('该场所正在审核');
        return;
      }
      wx.navigateTo({
        url: '/subcontract/equipment/pages/placeDetails/index?id=' + this.data.localeId
      });
    } else {
      wx.navigateTo({
        url: '/subcontract/equipment/pages/placeList/index'
      });
    }
  },
  // 点击扫码图标绑定呼叫器 - 仅用户
  openScanCode() {
    // var _this = this;
    wx.scanCode({
      success: res => {
        // pages/index/bindingEquipment/index?scene=ec=SL10000001
        const arr = res.path.split('scene=');
        const path = `${arr[0]}scene=${encodeURIComponent(arr[1])}&localeId=${this.data.localeId}`;
        wx.navigateTo({
          url: '/' + path
        });
      }
    });
  },
  // 获取场所列表
  async localeList(i) {
    let res = await $api.localeList({ filter: '' });
    res = res.data.data || [];
    const { localeId } = app.globalData;
    let target = res[0];
    if (localeId) {
      target = res.find(item => item.id == localeId) || target;
      if (i != 1) {
        delete app.globalData.localeId;
      }
    }
    this.setData({
      localeList: res,
      localeName: target.name,
      localeId: target.id,
      localeState: target.state,
      localeScrollTop: 0,
      localeMainFlag: target.mainFlag
    });
    this.getList();
  },
  onHide() {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    subPub.off('sleepSyncDetectionPush', this.sleepSyncDetectionPushId);
    subPub.off('deviceOffline', this.deviceOfflineId);
    subPub.off('deviceOnline', this.deviceOnlineId);
  }
  // 查看更多-商家
});
